import { useCallback } from 'react'
import { useDispatch } from 'react-redux'

type InTypes<T extends (...p: any[]) => any> = (
  ...p: Parameters<T>
) => ReturnType<T>

function useHandle<T extends(...p: any[]) => any>(p: T): InTypes<T> {
  const dispatch = useDispatch()
  const request = useCallback(
    (...t: Parameters<T>) => dispatch(p(...t)),
    [dispatch, p]
  )
  return request
}

export const useRequest = useHandle
